<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <home></home>
  </div>

  <template id="tpl">
    <div>
      <h2>我是home页</h2>
   
      <child1></child1>
      <hr>
      <child2></child2>
    </div>
  </template>

  <script src="./vue.js"></script>
  <script>
    // 创建第三方实例
    const bus = new Vue();
    const Child1 = {
      template:`
        <div>
          子组件1
         <button @click="emit">按钮1</button>
        </div>
      `,
      data(){
        return {
          msg: '我是子组件111的数据'
        }
      },
      methods: {
        emit(){
          // 触发一个自定义的事件
          bus.$emit('biubiubiu', this.msg)
        }
      }
    }

    const Child2 = {
      template:`
        <div>
         子组件2
        {{ msg }}
        </div>
      `,
      data(){
        return {
          msg:""
        }
      },
      mounted() {
        /* bus.$on("biubiubiu",(msg)=>{
          this.msg = msg
        })  */
        // let _this = this;
        /* bus.$on("biubiubiu", function(msg){
          // console.log(this)
          // this.msg = msg
          _this.msg = msg
        })  */
        bus.$on('biubiubiu', function(msg){
          this.msg = msg
        }.bind(this))        
      }
    }
    const Home = {
      template:"#tpl",
      components:{
        Child1,
        Child2
      },
      data(){
        return {
          msg:''
        }
      }
    }
    Vue.component('Home', Home)
  
    const vm = new Vue({
      el:'#app'
    })
  </script>
</body>
</html>